<template>
    <div class="programme" :style="{'--scale-factor':1.33333*1.4}">
        <div class="programme-content">
            <div class="programme-content-body">
                <div class="programme-content-title">
                    一、基本信息
                </div>
                <ProgrammeTable :columns="proJectInfoColumns" :data="projectInfo"/>
                <div class="programme-content-title">
                    二、本次申报
                </div>
                <ProgrammeTable :columns="bcsbInfoColumns" :data="bcsbInfo"/>
                <div class="programme-content-title">
                    三、前期准备
                </div>
                <ProgrammeTable :columns="qqzbInfoColumns" :data="projectInfo"/>
                <div class="programme-content-title">
                    四、投资计划
                </div>
                <ProgrammeTable :columns="tzjhCloumns" :data="projectInfo"/>
                <vxe-grid :="{
                    columns:tzjhGridColumns,
                    data:initTzjhData,
                    border:'none',
                    'header-row-class-name':'grid-table-header'
                }">
                </vxe-grid>
                <div class="programme-content-title">
                    五、收支预算
                </div>
                <ProgrammeTable :columns="szysCloumns" :data="projectInfo"/>
                <vxe-grid :="{
                    columns:szysColumns,
                    data:szysDataSource,
                    border:'none',
                    'header-row-class-name':'grid-table-header'
                }">
                </vxe-grid>
                <div style="margin-top: 30px;height: 20px;width: 100%"></div>
            </div>
        </div>
    </div>
</template>

<script setup>
import mock from "@/mock/programmeTableData.json"
import ProgrammeTable from "@/views/expert/online/drawer/ProgrammeTable.vue";
import RKUtils from "@/utils/rk-utils.js";
import {onMounted, ref} from "vue";
import SzysTableData from '@/mock/SzysTableData.json'
import { useProjectApi } from '@/api/index';
const { getReserveProjectInfoWithNameProId, queryBgtBalanceByProId, queryInvestPlanByProId } = useProjectApi();
const props = defineProps({
    proId: {
        type: String,
        required: true
    },
    applyId: {
        type: String,
        required: true
    }
})


const proJectInfoColumns = [
    {
        field: 'xmName',
        title: '项目名称',
        full: true
    },
    {
        field: 'xmCode',
        title: '项目编码',
    }, {
        field: 'lxYear',
        title: '立项年度',
    }, {
        field: 'useUnitId',
        title: '资产登记单位',
    }, {
        field: 'xmAddress',
        title: '项目地址',
    },
    {
        field: 'isFgwXm',
        title: '是否是重大项目库',
        render: (v) => {
            return v == 0 ? '否' : '是'
        }
    }, {
        field: 'fgwXmkCode',
        title: '发改委审批监管代码',
    }, {
        field: 'fgkzbfsId',
        title: '归口处室',
    },
    {
        field: 'gjzdzlxmId',
        title: '重大战略项目',
    }, {
        field: 'zjtxlyId',
        title: '资金投向邻域',
    }, {
        field: 'xmlxId',
        title: '项目类型',
    }, {
        field: 'xmzcqsId',
        title: '项目资产权属',
    }, {
        field: 'buildStatusId',
        title: '建设状态',
    }, {
        field: 'startDateActual',
        title: '开工日期',
    }, {
        field: 'endDateActual',
        title: '竣工日期',
    }, {
        field: 'buildContent',
        title: '项目主要建设内容和规模',
        full: true
    }
]

const bcsbInfoColumns = [
    {
        field: 'applyDate',
        title: '申报日期',
    }, {
        field: 'billYear',
        title: '申报年度',
    }, {
        field: 'bondTypeId',
        title: '债券类型',
    },
    {
        field: 'applyAmountTotal',
        title: '申请总金额',
    },
    {
        field: 'distributeAmount',
        title: '往年已发行金额',
    },
    {
        field: 'applyAmount1',
        title: '2023年申请金额',
    },
    {
        field: 'zqqxId',
        title: '债券期限',
    }, {
        field: 'zjtxlyId',
        title: '支出功能分类',
    }, {
        field: 'zjtxlyId',
        title: '支出经济分类',
    },
    {
        field: 'remark',
        title: '备注',
        full: true
    },
]
const qqzbInfoColumns = [
    {
        field: 'bcxx1',
        title: ' 是否已经主管部门立项审批',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    }, {
        field: 'bcxx2',
        title: '是否已完成环评工作',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    }, {
        field: 'bcxx3',
        title: ' 是否已完成安评工作',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    },
    {
        field: 'bcxx4',
        title: ' 是否已完成招投标工作',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    },
    {
        field: 'bcxx5',
        title: '是否已完成相关征地拆迁等相关工作',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    },
    {
        field: 'bcxx11',
        title: ' 是否取得《国有土地使用证》',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    },
    {
        field: 'bcxx12',
        title: '是否取得《建设用地规划许可证》',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    }, {
        field: 'bcxx13',
        title: ' 是否取得《建设工程规划许可证》',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    }, {
        field: 'bcxx14',
        title: '是否取得《建设工程施工许可证》',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    },
    {
        field: 'bcxx15',
        title: '实施方案中是否包含事前绩效评估内容',
        render: (item) => {
            return item == 0 ? '否' : '是'
        }
    },
]

const {projectInfo, bcsbInfo} = mock

const tzjhCloumns = [
    {
        field: 'bcxx15',
        title: '项目总概算',
        full: true
    },
    {
        field: 'bcxx15',
        title: '项目资本金(万元)',
    },
    {
        field: 'bcxx15',
        title: '其中财政预算安排(万元)',
    },
    {
        field: 'bcxx15',
        title: '专项债券安排（万元',
    },
]


const tzjhGridColumns = [
    {
        field: 'SET_YEAR',
        title: '年度',
        width: 120,
        align: 'center',
        formatter: ['formatAmountBit'],
    },
    {
        field: 'YEAR_SUM_AMT',
        title: '年度总投资',
        width: 150,
        align: 'right',
        formatter: ['formatAmountBit'],
    },
    {
        field: 'SUP_SUBS_PLAN_AMT',
        title: '上级补助资金-计划投资',
        width: 235,
        align: 'right',

    },
    {
        field: 'BUDG_PLAN_AMT',
        title: '本级财政预算资金-计划投资',
        width: 235,
        align: 'right',
        formatter: ['formatAmountBit'],

    },
    {
        field: 'FINA_PLAN_AMT',
        title: '融资资金-计划投资',
        width: 235,
        align: 'right',
        formatter: ['formatAmountBit'],

    },
    {
        field: 'MARK_PLAN_AMT',
        title: '市场化融资资金-计划投资',
        width: 235,
        align: 'right',
        formatter: ['formatAmountBit'],

    },
    {
        field: 'SELF_PLAN_AMT',
        title: '单位自筹资金-计划投资',
        width: 235,
        align: 'right',
        formatter: ['formatAmountBit'],

    },
    {
        field: 'OTHER_PLAN_AMT',
        title: '其他资金-计划投资',
        width: 235,
        align: 'right',
        formatter: ['formatAmountBit'],
    },
]

const initTzjhData = [{
    PLAN_ID: '',
    SET_YEAR: '',
    YEAR_SUM_AMT: 0,
    SUP_SUBS_PLAN_AMT: 0,
    BUDG_PLAN_AMT: 0,
    FINA_PLAN_AMT: 0,
    MARK_PLAN_AMT: 0,
    SELF_PLAN_AMT: 0,
    OTHER_PLAN_AMT: 0,
},]


const szysCloumns = [
    {
        field: 'bcxx15',
        title: '项目对应的政府性基金科目',
        full: true
    },
    {
        field: 'bcxx15',
        title: '开工日期',
    },
    {
        field: 'bcxx15',
        title: '建设期限（年）',
    },
    {
        field: 'bcxx15',
        title: '运营期限（年）',
    }, {
        field: 'bcxx15',
        title: '项目期限',
    }, {
        field: 'bcxx15',
        title: '项目资本金（万元）',
    }, {
        field: 'bcxx15',
        title: '其中财政预算安排（万元）',
    },
    {
        field: 'bcxx15',
        title: '专项债券安排（万元）',
    },
    {
        field: 'bcxx15',
        title: '备注',
        full: true
    },
]


const szysBasicColumns = [{
    field: 'BAL_TYPE_NAME',
    title: '收支类别',
    width: 200,
    align: 'left',
},
    {field: 'BAL_FORMULA', title: '公式', width: 160, align: 'left'},
    {
        field: "TOTAL", title: "合计", width: 300, sortable: false,
        children: [
            {
                field: 'SRAMT_Y',
                title: '收入',
                width: 150,
                align: "right",
                formatter: ['formatAmountBit'],
            },
            {
                field: 'ZCAMT_Y',
                title: '支出',
                width: 150,
                align: "right",
                formatter: ['formatAmountBit'],
            },
        ]
    }]
const szysColumns = ref([])

const formatAmount = (cellValue) => {
    return RKUtils.formatAmount(cellValue, 2, true)
}

/**
 * 处理动态列
 */
const handleDynamicColumn = () => {
    szysColumns.value = [...szysBasicColumns]
    // 建设期限，运营期限
    // const {PRO_BUID_TERM, PRO_OPER_TERM} = props.formData
    // let tremNum = Number(PRO_BUID_TERM) + Number(PRO_OPER_TERM)
    // if (!tremNum) {
    let tremNum = 4
    // }
    if (tremNum > 30) tremNum = 30
    let cDate = new Date()
    let yearStr = cDate.getFullYear() + "";
    let templateCol = {
        field: "TOTAL_Y23", title: "2023", width: 300, sortable: false,
        children: [
            {
                field: 'SRAMT_Y23',
                title: '收入',
                width: 150,
                align: "right",
            },
            {
                field: 'ZCAMT_Y23',
                title: '支出',
                width: 150,
                align: "right",
                formatter: ['formatAmountBit'],
            },
        ]
    }
    for (let i = 0; i <= tremNum; i++) {
        let yFullName = Number(yearStr) + i
        let tempCol = Object.assign({}, templateCol, {
            field: `TOTAL_Y${i}`, title: yFullName,
        })
        let tempChild_0 = Object.assign({}, tempCol.children[0], {
            field: `SRAMT_Y${i}`,
            // slots: {default: `rk_sr_${i}_input`, edit: `rk_sr_${i}_input_edit`}
        })
        let tempChild_1 = Object.assign({}, tempCol.children[1], {
            field: `ZCAMT_Y${i}`,
            // slots: {default: `rk_zc_${i}_input`}
        })
        Object.assign(tempCol, {children: [tempChild_0, tempChild_1]})
        szysColumns.value.push(tempCol)
    }
}

const tzjhDataSource = ref([])
const szysDataSource = ref(SzysTableData)

onMounted(() => {
    loadProjectInfo()
    loadSzysInfo()
    loadTzjhInfo()
    handleDynamicColumn()
})
const loadProjectInfo = () => {
    let params = {
        proId: props.proId,
        applyId: props.applyId
    }
    getReserveProjectInfoWithNameProId(params).then(resp => {
        // console.log(resp);
    })
}


const loadTzjhInfo = () => {
    let proId = props.proId
    if (proId) {
        queryInvestPlanByProId({proId: props.proId}).then(res => {
            if (res.code === 200) {
                tzjhDataSource.value = res.data
            }
        })

    }
}
const copyData = (data) => {
    let tempRows = []
    for (let row of SzysTableData) {
        let cRow = data.filter(item => {
            return item.BAL_FORMULA.trim() === row.BAL_FORMULA.trim()
        })
        let tempRow = row
        tempRow = Object.assign({}, cRow[0] || {}, row)
        if (cRow && cRow[0]) {
            for (let key in tempRow) {
                if (key.includes("SRAMT_Y") || key.includes("ZCAMT_Y")) {
                    tempRow[key] = cRow[0][key]
                }
            }
        }
        tempRows.push(tempRow)
    }
    return tempRows;
}
const loadSzysInfo = () => {
    let proId = props.proId
    if (proId) {
        queryBgtBalanceByProId({proId, proTypeId: ''})
            .then(resp => {
                if (resp.code === 200) {
                    szysDataSource.value = copyData(resp.data)
                } else {
                    globalProxy.$message.error(resp.msg)
                    szysDataSource.value = SzysTableData
                }
            })
    } else {
        szysDataSource.value = SzysTableData
    }
}

</script>

<style scoped lang="scss">
.programme {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #535659;
  display: flex;
  justify-content: center;


  &-content {
    background-color: #ffffff;
    width: 100%;
    display: flex;
    justify-content: center;
    overflow: auto;
    margin: 10px;

    &-title {
      font-family: 仿宋_GB2312;
      font-size: 16pt;
      font-style: normal;
      font-weight: bold;
      margin: 40px 0;
    }

    &-body {
      width: 80%;
    }
  }
}

:deep(.grid-table-header) {
  background-color: #ffffff;

}

:deep(.vxe-cell--title) {
  font-weight: normal !important;
}

:deep( td) {
  border: 1px solid #000 !important;
  border-collapse: collapse !important;
}

:deep(th) {
  border: 1px solid #000 !important;
  border-collapse: collapse !important;
}

:deep(table) {
  border: 1px solid #000 !important;
  border-collapse: collapse !important;
}

</style>
